import BaseAd from "../base/BaseAd";
import { SDKState, ADName, BannerDir, FunctionType } from "../SDKConfig";
/**
 * https://developers.weixin.qq.com/minigame/dev/api/ad/wx.createBannerAd.html
 * 创建 banner 广告组件。请通过 wx.getSystemInfoSync() 返回对象的 SDKVersion 
 * 判断基础库版本号 >= 2.0.4 后再使用该 API。每次调用该方法创建 banner 广告都会返回一个全新的实例。
 */
export default class WXBannerAd extends BaseAd {

    protected instance: wx.BannerAd;
    protected realSize;
    constructor(c) {
        super(c)
        this.load()
    }

    onResize(data) {
        // console.log('banner onResize', data)
        this.realSize = data;
        this.updateSize()
    }

    updateSize() {
        let size = this.realSize;
        if (!size) {
            return;
        }
        let winSize = wx.getSystemInfoSync();
        if (size.width != 0 && size.height != 0) {
            switch (this.dir) {
                case BannerDir.BOTTOM_LEFT:
                    this.instance.style.top = (winSize.windowHeight - size.height);
                    // this.instance.style.left = (winSize.windowWidth - size.width) / 2;
                    this.instance.style.left = 0;
                    break;
                case BannerDir.BOTTOM_MID:
                    this.instance.style.top = (winSize.windowHeight - size.height);
                    this.instance.style.left = (winSize.windowWidth - size.width) / 2;
                    break;
                case BannerDir.RIGHT_MID:
                    this.instance.style.top = (winSize.windowHeight - size.height) / 2;
                    this.instance.style.left = winSize.windowWidth - size.width;
                    break;
                case BannerDir.MID:
                    this.instance.style.top = (winSize.windowHeight - size.height) / 2;
                    this.instance.style.left = (winSize.windowWidth - size.width) / 2;
                    break;
                case BannerDir.MID_DOWN:
                    this.instance.style.top = (winSize.windowHeight - size.height) / 2 + 50;
                    this.instance.style.left = (winSize.windowWidth - size.width) / 2;
                    break;
            }

        }
    }

    load(isShow?: boolean) {
        this.logicState = isShow ? SDKState.open : SDKState.close;
        this.setState(SDKState.loading)
        this.destroy();
        this.create(this.channel.getRandomParem(ADName.banner))

    }




    open(adID: string) {

        // if (this.logicState == SDKState.open) {
        //     return;
        // }


        // this.logicState = SDKState.open;
        if (this.state == SDKState.loadSucess) {
            this.show();
        } else {
            this.load(true);
        }

    }

    close() {
        this.logicState = SDKState.close;
        this.hide()
        // }

    }

    show(): void {
        if (this.instance)
            this.instance.show();
    }

    hide(): void {
        if (this.instance)
            this.instance.hide();
    }

    getStyle(bannerWidth, bannerHeight) {
        let winSize = wx.getSystemInfoSync();
        switch (this.dir) {
            case BannerDir.BOTTOM_LEFT:
                return {
                    left: 0,
                    top: winSize.windowHeight - bannerHeight,
                    width: bannerWidth
                }
            case BannerDir.BOTTOM_MID:
                return {
                    left: (winSize.windowWidth - bannerWidth) / 2,
                    top: winSize.windowHeight - bannerHeight,
                    width: bannerWidth
                }
            case BannerDir.RIGHT_MID:
                return {
                    left: winSize.windowWidth - bannerWidth,
                    top: (winSize.windowHeight - bannerHeight) / 2,
                    width: bannerWidth
                }
        }

    }


    destroy(): void {
        if (this.instance) {
            this.instance.offLoad(this.getFunc(FunctionType.onLoad))
            this.instance.offError(this.getFunc(FunctionType.onError))
            this.instance.offResize(this.getFunc(FunctionType.onResize))
            this.instance.destroy()
            this.instanceMap[this.adUnitID] = null;
        }
    }
    protected create(adID, param?): void {
        this.adUnitID = adID;
        let bannerHeight = 100;
        let bannerWidth = 200;
        if (!this.instanceMap[adID]) {
            this.instance = wx.createBannerAd({
                adUnitId: adID,
                adIntervals: 30,
                style: this.getStyle(bannerWidth, bannerHeight)
            })
            this.instance.onLoad(this.getFunc(FunctionType.onLoad))
            this.instance.onError(this.getFunc(FunctionType.onError))
            this.instance.onResize(this.getFunc(FunctionType.onResize))
            this.instanceMap[adID] = this.instance;
        }


    }


    onError(err) {
        console.log('banner onError', err)
        this.setState(SDKState.loadFail)
        if (this.callback) {
            this.callback(0)
        }
        // setTimeout(() => {
        //     this.load()
        // }, 3000);
    }

    onLoad() {
        console.log('banner onLoad', this.logicState)
        this.setState(SDKState.loadSucess)
        if (this.callback) {
            this.callback(1)
        }
        if (this.logicState == SDKState.open) {
            this.show();
        }
        // setTimeout(() => {
        //     this.load()
        // }, 30000);
    }

}
